package com.bilibili.server.service;

import com.bilibili.dubbo.api.db.UserInfoApi;
import com.bilibili.dubbo.api.mongo.CommentApi;
import com.bilibili.model.db.UserInfo;
import com.bilibili.model.enums.CommentType;
import com.bilibili.model.mongo.Comment;
import com.bilibili.model.vo.CommentVo;
import com.bilibili.model.vo.PageResult;
import com.bilibili.server.interceptor.UserHolder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HuangRongFu
 * 2021/11/22 星期一5:56 下午
 */

@Service
public class CommentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserFreezeService userFreezeService;

    @Autowired
    private MqMessageService mqMessageService;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 查看评论列表
     *
     * @param: page
     * @param: pageSize
     * @param: movementId
     * @Return: ResponseEntity
     */
    public PageResult<CommentVo> findComments(Long page, Long pageSize, String movementId) {

        //通过动态的id 去分页查询评论
        PageResult pageResult = commentApi.findPageByComment(page, pageSize, movementId);

        //获取评论结果集
        List<Comment> commentList = pageResult.getItems();

        List<CommentVo> voList = new ArrayList<>();

        //对结果集进行非空判断
        //不为空
        if (CollectionUtils.isNotEmpty(commentList)) {

            //取出用户的id
            List<Long> ids = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());

            //批量查询用户的详细信息
            Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(ids, null);

            //转化成voList
            voList = commentList.stream().map(u -> CommentVo.init(userInfoMap.get(u.getUserId()), u))
                    .collect(Collectors.toList());
        }

        //设置结果集
        pageResult.setItems(voList);

        return pageResult;
    }

    /**
     * 发布评论
     *
     * @param: paramMap
     * @Return: ResponseEntity
     */
    public void addComment(Map<String, String> paramMap) {

        userFreezeService.checkUserStatus(2, UserHolder.getUser().getMobile());

        //构建对象
        Comment comment = new Comment();

        comment.setId(new ObjectId());
        comment.setPublishId(new ObjectId(paramMap.get("movementId")));//设置动态id
        comment.setContent(paramMap.get("comment"));//设置评论的内容
        comment.setUserId(UserHolder.getUserId());//评论人的id
        comment.setCommentType(CommentType.COMMENT.getType());

        commentApi.add(comment);

        mqMessageService.sendLogMessage(UserHolder.getUserId(), "0205", "movement", comment.getId().toHexString());
    }

}
